import json
import random

from django.http import HttpResponse, HttpResponseBadRequest, JsonResponse
from django.shortcuts import render
import os

from django.urls import reverse
from django.views import View

from demo.settings import BASE_DIR


# Create your views here.
def index(request, param):
    print(request)
    return HttpResponse('index')


def qs(request):
    print(request)
    name = request.GET.get('name')
    return HttpResponse(name)


def body(request):
    print(request)
    print(request.FILES)
    content = request.FILES.get('1').file
    file_name = request.FILES.get('1').name
    with open(file_name, 'wb') as f:
        f.write(content.read())
    return HttpResponse('body')


from django.shortcuts import redirect


def my_response(request):
    resp = HttpResponse()
    # resp.content = 'my_response'
    # # resp['Content-Type'] = 'text/html'
    # resp['Location'] = '/demo/qs/'
    # resp.status_code = 301
    # return HttpResponseBadRequest()
    return redirect('/demo/qs/')


def user(request):
    url = reverse('demo_login')
    return redirect(url)


def image(request):
    with open('1.mp4', 'rb') as f:
        content = f.read()
    return HttpResponse(content, content_type='video/mp4')


def json_resp(request):
    # json_str = '''{
    # "name":"zhangsan",
    # "age":"18"}
    # '''
    d = {
        "name": 'zhangsan',
        "age": 18
    }
    # json_str = json.dumps(d)
    # return HttpResponse(json_str, content_type='application/json')  #
    return JsonResponse(d)
    # return HttpResponse(json_str, content_type='text/html')


def user_center(request):
    # 1. 校验用户名密码
    # name = request.GET.get('name')
    # pwd = request.GET.get('pwd')
    token = request.GET.get('token')  # zhangsan|123
    data = token.split('|')  # ['zhangsan','123']

    if data[0] == 'zhangsan' and data[1] == '123':
        return HttpResponse('余额100元')
        # 2. 返回用户余额
    return HttpResponse('账号密码错误', status=401)


def user_hobby(request):
    # 1. 校验用户名密码
    name = request.GET.get('name')
    pwd = request.GET.get('pwd')

    if name == 'zhangsan' and pwd == '123':
        return HttpResponse('你的爱好是足球,游泳')
        # 2. 返回用户余额
    return HttpResponse('账号密码错误', status=401)


def login(request):
    name = request.GET.get('name')
    pwd = request.GET.get('pwd')

    token = '%s|%s' % (name, pwd)
    resp = HttpResponse('登录成功')
    resp.set_cookie('token', token)
    return resp


# http://127.0.0.1:8000/demo/zhuanzhang/
# C:\xxx\xxx\yy.html
def zhuanzhang(request):
    if request.method == 'GET':
        # 返回转账页面
        csrf_token = random.randint(1000, 9999)
        # 1. 设置在表单里
        # 2. 设置在cookie中
        resp = render(request, 'zhuanzhang.html', {"csrf_token": csrf_token})
        resp.set_cookie('csrf_token', csrf_token)
        return resp
    if request.method == 'POST':
        # 处理转账请求
        # 1. 取用户cookie
        cookie = request.COOKIES
        # 2. 取用户信息
        token = cookie.get('token')
        if token:
            username = token.split('|')[0]
        else:
            return HttpResponse('请登录')

        # 校验csrf_token 是否匹配
        cookie_csrf_token = cookie.get('csrf_token')
        form_csrf_token = request.POST.get('csrf_token')

        if cookie_csrf_token == form_csrf_token:
            pass
        else:
            return HttpResponse('非法请求')

        # 3. 取转账账号信息
        to = request.POST.get('to')
        amount = request.POST.get('amount')
        # 4. 转账
        s = "%s给%s转账%s钱" % (username, to, amount)
        print(s)
        # 5. 返回结果
        return HttpResponse(s)


def hkzz(request):
    return render(request, 'hkzz.html')


def session_set(request):
    name = request.GET.get('name')
    request.session['name'] = name
    return HttpResponse('session 设置成功')


def session_get(request):
    name = request.session.get('name')
    return HttpResponse(name)


def session_clear(request):
    request.session.clear()
    return HttpResponse('session内容已清除')


def session_flush(request):
    request.session.flush()
    return HttpResponse('session已删除')


class IndexView(View):
    def get(self, request):
        return HttpResponse('class view get')


class MiddleView(View):
    def get(self, request):
        print('view')
        if request.username == 'zhangsan' and request.pwd == '123':
            pass
        return HttpResponse('middle ware view')
